package com.jia.sercurity;

import com.jia.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.password.Pbkdf2PasswordEncoder;

@Configuration
@EnableWebSecurity
public class MySecurityConfiguration extends WebSecurityConfigurerAdapter {
    @Autowired
    private MyService myService;
    @Bean
    public Pbkdf2PasswordEncoder passwordEncoder(){
        return new Pbkdf2PasswordEncoder("ahead");
    }
    @Override
    //登录权限
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myService).passwordEncoder(passwordEncoder());
    }
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
                .antMatchers("/js/**","/ShowFIndDate.html","/find/**","/show/**") //代表必须通过认证才可以访问 去掉之后则不需要认证就可以访问了
                .authenticated()
                .anyRequest()
                .permitAll()
                .and()
                .formLogin()
                .loginPage("/login.html")
                .loginProcessingUrl("/login")
                .successForwardUrl("/find/showuser")
                .failureForwardUrl("/find/showerror")
                .and()
                .csrf()
                .disable();
    }
}
